﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Web.Security;
using JFLogic.Utils;
using Model;
using JFLogic.Wrapper;
using JFLogic.Logic.CodeRepo;
using JFLogic.Logic.JF;

namespace CreateRadonRepo
{
    class Program
    {
        static Timer timer;
        static Timer reportErrTimer;
        static CodeRepoLogic rLogic = null;
        static void Main(string[] args)
        {
            Log.PrintLog("预先加载积分二维码凭证程序开始运行！！！！！");
            timer = new Timer(10000);
            timer.Elapsed += timer_Elapsed;
            timer.Start();

            reportErrTimer = new Timer(60000);
            reportErrTimer.Elapsed += reportErrTimer_Elapsed;
            reportErrTimer.Start();

            rLogic = new CodeRepoLogic();
            string str = "";
            while (str != "exit")
            {
                str = Console.ReadLine();
            }
        }

        static void reportErrTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                reportErrTimer.Stop();

                CodeRepoLogic cpLogic = new CodeRepoLogic();
                
                TB_Code_Repo tempCodeRepo = cpLogic.GetFailRepo();
                if (tempCodeRepo != null)
                {
                    int result = WrapperUtil.getInstance().ReportErr((int)tempCodeRepo.CodeId);
                    tempCodeRepo.ReportState = result;
                    cpLogic.Update(tempCodeRepo);    
                }
                

            }
            catch (Exception ex)
            {
                Log.PrintLog(ex.Message);
            }
            finally
            {
                reportErrTimer.Start();
            }
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                timer.Stop();
                Log.PrintLog("定时器开始执行");
                int storeRepoCount = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["StoreRepoCount"]);
                int RadonTimeOut = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["RadonTimeOut"]);
                if (rLogic.getUnUsedRepoCount(RadonTimeOut) < storeRepoCount)
                {
                    string sId = FormsAuthentication.HashPasswordForStoringInConfigFile(Guid.NewGuid().ToString(), "MD5");
                    Log.PrintLog("开始获取验证码");
                    JFBLL jfBll = new JFBLL("", "", sId);
                    var cpInfo = new TB_Code_Repo()
                    {
                        CodeState = CodeState.NOTSTART,
                        CodeStr = "",
                        SessionId = sId,
                        STime = DateTime.Now

                    };
                    rLogic.Save(cpInfo);
                    bool suc = jfBll.GetRadon();
                    if (suc)
                    {
                        cpInfo.CodeState = CodeState.DOWNSUCCESS;
                        rLogic.Update(cpInfo);
                    }
                    else
                    {
                        Log.PrintLog("下载验证码图片失败");
                        return;
                    }
                    Log.PrintLog("开始识别验证码");

                    CodeInfo cInfo = jfBll.RecognizeRadon();
                    string checkResult = CodeUtils.CheckCode(cInfo.ResultCode);
                    cpInfo.CodeStr = cInfo.ResultCode;
                    cpInfo.CodeId = cInfo.CodeId;
                    if (string.IsNullOrEmpty(checkResult))
                    {//识别成功
                        cpInfo.CodeState = CodeState.RECOGNIZE;
                    }
                    else
                    {
                        Log.PrintLog(string.Format("识别错误：codeid:{0},识别结果:{1}", cInfo.CodeId, checkResult));
                        cpInfo.CodeState = CodeState.FAILED;

                    }
                    rLogic.Update(cpInfo);
                    Log.PrintLog("识别完成");
                }
            }
            catch (Exception ex)
            {
                Log.PrintLog(ex.Message);
            }
            finally
            {
                timer.Start();
            }
        }
    }
}
